import riskClient from "./riskClient";

import { session } from "@core";

// 获取字典表
export const queryCfgCodeList = async () => {

  const {data, error} = await riskClient.postAsync({
    url: 'api/common/queryCfgCodeList',
  })

  if (error) return []

  // const codes = data?.map((item) => {
  //   const codeObj = {
  //     codeKey: item.codeKey,
  //     codeDesc: item.codeDesc,
  //     options: item.options?.map((oItem) => ({
  //       value: oItem.value,
  //       label: oItem.label,
  //     })),
  //   }
  //   return codeObj
  // })

  return data

}

// 获取用户权限
export const getUserMenuAuthCode = async () => {
  const userCode = session.get('userCode')
  
  const {data, error} = await riskClient.postAsync({
    url: 'auth/getUserMenuAuthCode',
    params: { userCode },
  })
  if (error) return []

  return data.keyCodeList
}

// export const commonFindUnit = async () => {

//   const {data, error} = await riskClient.postAsync({
//     url: 'api/common/findUnit',
//   })

//   if (error) return []

//   const unit = {
//     codeKey: 'unit',
//     codeDesc: '用户权限机构',
//     options: data?.map((item) => ({
//       value: item.unitCode,
//       label: item.unitName,
//     })),
//   }

//   return [unit]

// }

// export const findUnits = async () => {

//   const {data, error} = await riskClient.postAsync({
//     url: 'api/common/findUnits',
//   })

//   if (error) return []

//   const unit = {
//     codeKey: 'units',
//     codeDesc: '用户归属机构',
//     options: data?.map((item) => ({
//       value: item.unitCode,
//       label: item.unitName,
//     })),
//   }

//   return [unit]

// }

// export const findUserAuthUnits = async () => {

//   const {data, error} = await riskClient.postAsync({
//     url: 'api/common/findUserAuthUnits',
//   })

//   if (error) return []

//   const unit = {
//     codeKey: 'userAuthUnits',
//     codeDesc: '用户权限机构',
//     options: data?.map((item) => ({
//       value: item.unitCode,
//       label: item.unitName,
//     })),
//   }

//   return [unit]

// }

// export const findUnitProductType = async () => {
//   const {data, error} = await riskClient.getAsync({
//     url: 'api/common/findRiskList',
//     restfull: false
//   })
//   if (error) return []

//   const Areas = {
//     codeKey: 'productTypes',
//     codeDesc: '产品类型',
//     options: data
//   }

//   return [Areas]
// }

// 获取天气类型等级
export const findWeatherTypeGrade = async (params) => {
  const {data, error} = await riskClient.postAsync({
    url: 'api/common/findWeatherTypeGrade',
    params
  })
  if (error) return []

  return data.options
}

// 根据机构获取对应的风险区域
export const findUnitArea = async (params) => {
  const {data, error} = await riskClient.getAsync({
    url: 'api/common/findUnitArea',
    params,
  })
  if (error) return []

  const Areas = data?.map((item) => ({
      value: item.cfgAreaId,
      label: item.cfgAreaName,
    }))

  return Areas
}

// 根据机构获取对应的产品类型
export const findUnitRisks = async (params) => {
  const {data, error} = await riskClient.getAsync({
    url: 'api/common/findUnitRisks',
    params,
  })
  if (error) return []

  const Areas = data?.map((item) => ({
      value: item.productCode,
      label: item.productType,
    }))

  return Areas
}

// 获取省市区
export const getAddressFindRegion = async (params) => {
  return await riskClient.postAsync({
    url: 'api/address/findRegion',
    params,
  })
};